package com.example.resultinterceptor.service.impl;

import com.example.resultinterceptor.entity.po.User;
import com.example.resultinterceptor.mapper.UserMapper;
import com.example.resultinterceptor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.sql.SQLException;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Long id) {
        return userMapper.findById(id);
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User getUserByEmail(String email) {
        return userMapper.findByEmail(email);
    }

    @Override
    public List<User> getActiveUsers() {
        return userMapper.findActiveUsers();
    }

    @Override
    public List<User> getUsersByNickname(String nickname) {
        return userMapper.findByNicknameLike("%" + nickname + "%");
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.findAll();
    }

    @Override
    public User saveUser(User user) {
        if (user.getCreateTime() == null) {
            user.setCreateTime(LocalDateTime.now());
        }
        if (user.getUpdateTime() == null) {
            user.setUpdateTime(LocalDateTime.now());
        }
        return userMapper.save(user);
    }

    @Override
    public boolean updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.update(user) > 0;
    }

    @Override
    public boolean deleteUser(Long id) {
        return userMapper.deleteById(id) > 0;
    }

    // ========== JDBC 报错示例方法实现 ==========

    @Override
    public List<User> testSqlSyntaxError() {
        // 故意使用错误的SQL语法
        return userMapper.testSqlSyntaxError();
    }

    @Override
    public List<User> testTableNotExistsError() {
        // 查询不存在的表
        return userMapper.testTableNotExistsError();
    }

    @Override
    public List<User> testColumnNotExistsError() {
        // 查询不存在的字段
        return userMapper.testColumnNotExistsError();
    }

    @Override
    public User testDataTypeError(String invalidId) {
        // 尝试将字符串转换为Long类型的ID
        try {
            Long id = Long.parseLong(invalidId);
            return userMapper.findById(id);
        } catch (NumberFormatException e) {
            throw new RuntimeException("数据类型转换失败: " + invalidId + " 不是有效的数字", e);
        }
    }

    @Override
    public User testDuplicateKeyError(User user) {
        // 尝试插入重复的唯一键
        return userMapper.testDuplicateKeyError(user);
    }

    @Override
    public List<User> testConnectionTimeoutError() {
        // 模拟数据库连接超时
        return userMapper.testConnectionTimeoutError();
    }

    @Override
    public String testDeadlockError() {
        // 模拟死锁情况
        return userMapper.testDeadlockError();
    }

    @Override
    public List<User> testPSQLException() {
        // 测试PostgreSQL特定异常
        return userMapper.testPSQLException();
    }
}
